<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>用户登录</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Microsoft YaHei', Arial, sans-serif;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
        }

        .login-container {
            background: rgba(255, 255, 255, 0.95);
            padding: 3rem;
            border-radius: 20px;
            box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
            width: 100%;
            max-width: 400px;
            backdrop-filter: blur(10px);
        }

        .login-header {
            text-align: center;
            margin-bottom: 2rem;
        }

        .login-header h1 {
            color: #333;
            margin-bottom: 0.5rem;
        }

        .login-header p {
            color: #666;
        }

        .form-group {
            margin-bottom: 1.5rem;
        }

        .form-group label {
            display: block;
            margin-bottom: 0.5rem;
            color: #333;
            font-weight: 500;
        }

        .form-group input {
            width: 100%;
            padding: 12px 15px;
            border: 1px solid #ddd;
            border-radius: 8px;
            font-size: 1rem;
            transition: border-color 0.3s ease;
        }

        .form-group input:focus {
            outline: none;
            border-color: #667eea;
            box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
        }

        .captcha-container {
            display: flex;
            gap: 10px;
        }

        .captcha-input {
            flex: 1;
        }

        .captcha-image {
            width: 120px;
            height: 45px;
            background-color: #f0f0f0;
            border: 1px solid #ddd;
            border-radius: 8px;
            display: flex;
            align-items: center;
            justify-content: center;
            font-weight: bold;
            font-size: 1.2rem;
            color: #333;
            cursor: pointer;
            user-select: none;
        }

        .btn {
            width: 100%;
            padding: 12px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            border: none;
            border-radius: 8px;
            font-size: 1rem;
            font-weight: bold;
            cursor: pointer;
            transition: transform 0.3s ease, box-shadow 0.3s ease;
        }

        .btn:hover {
            transform: translateY(-2px);
            box-shadow: 0 10px 20px rgba(0, 0, 0, 0.2);
        }

        .login-footer {
            text-align: center;
            margin-top: 1.5rem;
            color: #666;
        }

        .login-footer a {
            color: #667eea;
            text-decoration: none;
        }

        .login-footer a:hover {
            text-decoration: underline;
        }
        
        .loading {
            opacity: 0.7;
            pointer-events: none;
        }
        
        /* 自定义模态弹窗样式 */
        .modal {
            display: none;
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background-color: rgba(0, 0, 0, 0.5);
            z-index: 1001;
            justify-content: center;
            align-items: center;
        }
        
        .modal-content {
            background: white;
            border-radius: 15px;
            box-shadow: 0 20px 40px rgba(0, 0, 0, 0.2);
            max-width: 400px;
            width: 90%;
            text-align: center;
            animation: modalOpen 0.3s ease;
        }
        
        @keyframes modalOpen {
            from {
                opacity: 0;
                transform: translateY(-50px);
            }
            to {
                opacity: 1;
                transform: translateY(0);
            }
        }
        
        .modal-header {
            padding: 20px;
            border-radius: 15px 15px 0 0;
            color: white;
        }
        
        .success .modal-header {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        
        .error .modal-header {
            background: linear-gradient(135deg, #ff6b6b 0%, #ee5a52 100%);
        }
        
        .modal-body {
            padding: 30px 20px;
            font-size: 1.1rem;
            color: #333;
        }
        
        .modal-footer {
            padding: 20px;
        }
        
        .modal-btn {
            padding: 10px 30px;
            border: none;
            border-radius: 25px;
            font-weight: bold;
            cursor: pointer;
            transition: all 0.3s ease;
        }
        
        .success .modal-btn {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
        }
        
        .error .modal-btn {
            background: linear-gradient(135deg, #ff6b6b 0%, #ee5a52 100%);
            color: white;
        }
        
        .modal-btn:hover {
            transform: translateY(-2px);
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.2);
        }
        
        .icon {
            font-size: 3rem;
            margin-bottom: 15px;
        }
        
        .success .icon {
            color: #4caf50;
        }
        
        .error .icon {
            color: #f44336;
        }
    </style>
</head>
<body>
    <div class="login-container">
        <div class="login-header">
            <h1>用户登录</h1>
            <p>请输入您的账号和密码</p>
        </div>
        <form id="loginForm">
            <div class="form-group">
                <label for="username">用户名</label>
                <input type="text" id="username" name="username" required>
            </div>
            <div class="form-group">
                <label for="password">密码</label>
                <input type="password" id="password" name="password" required>
            </div>
            <div class="form-group">
                <label for="captcha">验证码</label>
                <div class="captcha-container">
                    <input type="text" id="captcha" name="captcha" class="captcha-input" required maxlength="4">
                    <div class="captcha-image" id="captchaImage" title="点击刷新验证码">加载中...</div>
                </div>
            </div>
            <button type="submit" class="btn">登录</button>
        </form>
        <div class="login-footer">
            <p>还没有账号？<a href="#">立即注册</a></p>
        </div>
    </div>
    
    <!-- 成功提示模态框 -->
    <div id="successModal" class="modal success">
        <div class="modal-content">
            <div class="modal-header">
                <div class="icon">✓</div>
                <h2>登录成功</h2>
            </div>
            <div class="modal-body">
                <p>欢迎您，admin！</p>
                <p>即将跳转到首页...</p>
            </div>
            <div class="modal-footer">
                <button class="modal-btn" onclick="closeSuccessModal()">立即跳转</button>
            </div>
        </div>
    </div>
    
    <!-- 失败提示模态框 -->
    <div id="errorModal" class="modal error">
        <div class="modal-content">
            <div class="modal-header">
                <div class="icon">✕</div>
                <h2>登录失败</h2>
            </div>
            <div class="modal-body">
                <p id="errorMessage">用户名或密码错误</p>
            </div>
            <div class="modal-footer">
                <button class="modal-btn" onclick="closeErrorModal()">我知道了</button>
            </div>
        </div>
    </div>

    <script>
        // 页面加载完成后获取验证码
        document.addEventListener('DOMContentLoaded', function() {
            refreshCaptcha();
            
            // 点击验证码图片刷新验证码
            document.getElementById('captchaImage').addEventListener('click', refreshCaptcha);
        });

        // 刷新验证码
        function refreshCaptcha() {
            fetch('/captcha')
                .then(response => response.json())
                .then(data => {
                    if (data.code === 1) {
                        document.getElementById('captchaImage').textContent = data.data.captcha;
                    } else {
                        document.getElementById('captchaImage').textContent = '错误';
                    }
                })
                .catch(error => {
                    console.error('获取验证码时出错:', error);
                    document.getElementById('captchaImage').textContent = '错误';
                });
        }
        
        // 显示成功模态框
        function showSuccessModal() {
            document.getElementById('successModal').style.display = 'flex';
        }
        
        // 关闭成功模态框并跳转
        function closeSuccessModal() {
            document.getElementById('successModal').style.display = 'none';
            window.location.href = '/index.html';
        }
        
        // 显示错误模态框
        function showErrorModal(message) {
            document.getElementById('errorMessage').textContent = message;
            document.getElementById('errorModal').style.display = 'flex';
        }
        
        // 关闭错误模态框
        function closeErrorModal() {
            document.getElementById('errorModal').style.display = 'none';
        }

        // 处理表单提交
        document.getElementById('loginForm').addEventListener('submit', function(e) {
            e.preventDefault();
            
            const username = document.getElementById('username').value;
            const password = document.getElementById('password').value;
            const captcha = document.getElementById('captcha').value;
            
            // 禁用提交按钮并显示加载状态
            const submitBtn = document.querySelector('.btn');
            submitBtn.textContent = '登录中...';
            document.querySelector('.login-container').classList.add('loading');
            
            // 创建登录请求数据
            const loginData = {
                username: username,
                password: password,
                captcha: captcha
            };
            
            // 发送POST请求到/login端点
            fetch('/login', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify(loginData)
            })
            .then(response => response.json())
            .then(data => {
                if (data.code === 1) {
                    // 登录成功
                    showSuccessModal();
                    // 3秒后自动跳转
                    setTimeout(function() {
                        window.location.href = '/index.html';
                    }, 3000);
                } else {
                    // 登录失败
                    showErrorModal('登录失败：' + data.msg);
                    // 刷新验证码
                    refreshCaptcha();
                    // 清空验证码输入框
                    document.getElementById('captcha').value = '';
                    document.getElementById('captcha').focus();
                }
            })
            .catch(error => {
                console.error('Error:', error);
                showErrorModal('登录过程中出现错误，请稍后重试');
                // 刷新验证码
                refreshCaptcha();
            })
            .finally(() => {
                // 恢复提交按钮状态
                submitBtn.textContent = '登录';
                document.querySelector('.login-container').classList.remove('loading');
            });
        });
        
        // 点击模态框外部关闭模态框
        window.onclick = function(event) {
            const successModal = document.getElementById('successModal');
            const errorModal = document.getElementById('errorModal');
            
            if (event.target === successModal) {
                closeSuccessModal();
            }
            
            if (event.target === errorModal) {
                closeErrorModal();
            }
        }
    </script>
</body>
</html>